package edu.vt.ece_3574.amber_alert;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import edu.vt.ece_3574.amber_alert.accounts.Account;
import edu.vt.ece_3574.amber_alert.accounts.Friend;
import edu.vt.ece_3574.amber_alert.membership.MemberAccountSettings;
import edu.vt.ece_3574.amber_alert.membership.PrimaryAccountSettings;
import edu.vt.ece_3574.amber_alert.messaging.MessagingUtils;

public class ExistingHangoutActivity extends Activity{
	public static String target;
	
	Spinner hangoutSelectorSpinner;
	
	public static Friend selectedFriend;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.choose_existing_hangout);
		
		populateSpinner();
		
		Button returnButton = (Button) findViewById(R.id.returnToMemberMain);
		returnButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				
				Intent intent = new Intent(ExistingHangoutActivity.this, MemberMain.class);
				ExistingHangoutActivity.this.finish();
				startActivity(intent);
				
			}
		});
		
		Button sendHangout = (Button) findViewById(R.id.sendToParent);
		sendHangout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				selectedFriend = AmberAlertApplication.get().getCurrentAccount().getFriend((String) hangoutSelectorSpinner.getSelectedItem());;
				String hangoutMessage = "I am now with " + selectedFriend.getName() + ". Their number is" + selectedFriend.getNumber() 
						+ " and their address is " + selectedFriend.getAddress();
				ArrayList<Account> primaries = AmberAlertApplication.get().getCurrentAccount().getAssociatedAccounts();
				if (primaries.size() == 0) {
					Intent i = new Intent(ExistingHangoutActivity.this, MemberAccountSettings.class);
					startActivity(i);
				}
				else {
					for (Account primaryNumber : primaries) {
						MessagingUtils.get().sendSMS(primaryNumber.getNumber(), hangoutMessage);
					}
				}
				Intent i = new Intent(ExistingHangoutActivity.this, MemberMain.class);
				ExistingHangoutActivity.this.finish();
				startActivity(i);
			}
		});
		
		
		
		
	}
	
	private void populateSpinner() {
		hangoutSelectorSpinner = (Spinner) findViewById(R.id.hangoutSpinner);
		
		ArrayAdapter <CharSequence> adapter = new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		for (String friendName : AmberAlertApplication.get().getCurrentAccount().getAssociatedFriendNames()) {
			adapter.add(friendName);
		}
		
		hangoutSelectorSpinner.setAdapter(adapter);
	}
	
	public void onBackPressed() {
		Intent i = new Intent(ExistingHangoutActivity.this, MemberMain.class);
		finish();
		startActivity(i);
		return;
	}
}
